﻿#include "mainmanagerservice.h"
#include "apphelper.h"
#include <QDebug>
#include <qtextcodec.h>




const QString configPath = "/../config/Plugins.xml";

MainManagerService::MainManagerService(CFSPluginContext *pContext)
    : _context(pContext)
{
    //    pContext->registerService<IMainManagerService>(this);
    QDir path1(QCoreApplication::applicationDirPath());
    QString dir = path1.absolutePath();
    xmlPath = dir + configPath;
}

MainManagerService::~MainManagerService()
{

}

void MainManagerService::installPlugins(CFSPluginContext* pContext)
{
    //解析xml，加载插件
    QDir currentPath(QCoreApplication::applicationDirPath());
    QString dir = currentPath.absolutePath();

    QString xmlPath = dir + configPath;
    QList<MainFrameGlobal::XMLPluginData> xmlList;

    readPluginXml(xmlPath, &xmlList);


    for (int i = 0; i < xmlList.count();i++)
    {
        try
        {
            if(xmlList[i].pluginState == MainFrameGlobal::UNINSTALL)
            {
                //插件注册
                QSharedPointer<CFSPlugin> plugin = pContext->installPlugin(QUrl::fromLocalFile(dir + "/plugins/" +xmlList[i].path));

                if(plugin)
                {
                    plugin->start(CFSPlugin::START_TRANSIENT);
                }
                else
                {
                    qDebug()<<xmlList[i].name <<"Plugin Install Failed! ******";
                }
            }
            else if(xmlList[i].pluginState == MainFrameGlobal::INSTALL)
            {
                qDebug()<<xmlList[i].name <<"Plugin already Installed! +++++";
                continue;
            }

        }
        catch(const CFSException &e)
        {
            qDebug()<<"****** Failed to install plugin : "<<xmlList[i].path << ",error info:"<<e.what() <<"******";
            return;
        }
    }

}

void MainManagerService::changeXml(const int &row)
{
    QFile file(xmlPath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return;
    }
    QDomDocument doc;
    if (doc.setContent(&file))
        file.close();
    else
        return;

    QDomElement root = doc.documentElement();
    QDomNode node = root.firstChild();
    QDomNodeList list = node.toElement().childNodes();

    QDomNode oldnode = list.at(row);

    int state = oldnode.toElement().attribute("PluginState").toInt();
    if(state == 0)
        oldnode.toElement().setAttribute("PluginState","1");
    else if(state == 1)
        oldnode.toElement().setAttribute("PluginState","0");

    if(!file.open(QFile::WriteOnly | QFile::Truncate))
        return;
    QTextStream out_stream(&file);
    doc.save(out_stream,4);
    file.close();
}

void MainManagerService::readPluginXml(const QString &path, QList<MainFrameGlobal::XMLPluginData>* xmlList)
{
    qDebug() << "plugins xml path: " << path;
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QDomDocument doc;
    if (doc.setContent(&file))
        file.close();
    else
        return;

    QDomElement root = doc.documentElement();
    QDomNode node = root.firstChild();
    while (!node.isNull())
    {
        QString tagName = node.toElement().tagName();
        if(tagName == "BasePlugins" || tagName == "NormalPlugins")
        {
            QDomNodeList list = node.childNodes();
            for(int i = 0; i < list.size(); ++i)
            {
                QDomNode node = list.at(i);
                if(node.toElement().tagName() != nullptr)
                {
                    MainFrameGlobal::XMLPluginData pluginObj;

                    pluginObj.displayName = node.toElement().attribute("DisplayName");
                    pluginObj.version	  = node.toElement().attribute("Version");
                    pluginObj.name		  = node.toElement().attribute("Name");
                    pluginObj.path		  = node.toElement().attribute("Path");
                    pluginObj.iconPath	  = node.toElement().attribute("IconPath");
                    int state = node.toElement().attribute("PluginState").toInt();

                    if(state == 0)
                        pluginObj.pluginState = MainFrameGlobal::INSTALL;
                    else if(state == 1)
                        pluginObj.pluginState = MainFrameGlobal::UNINSTALL;

                    if(tagName == "BasePlugins")
                        pluginObj.pluginType = MainFrameGlobal::BASE;
                    else
                        pluginObj.pluginType = MainFrameGlobal::NORMAL;

                    xmlList->append(pluginObj);
                    qDebug() << pluginObj.name << "+++++";
                }
            }
        }
        node = node.nextSibling();
    }
}



